package org.itzixi.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.itzixi.RedisKeyConstant;
import org.itzixi.RequestConstant;
import org.itzixi.grace.result.GraceJSONResult;
import org.itzixi.grace.result.ResponseStatusEnum;
import org.itzixi.pojo.Users;
import org.itzixi.pojo.bo.ModifyUserBO;
import org.itzixi.pojo.vo.UsersVO;
import org.itzixi.service.UsersService;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/userinfo")
@Slf4j
public class UserController {

    @Resource
    private UsersService usersService;


    @PostMapping("/modify")
    public GraceJSONResult modify(@RequestBody @Valid ModifyUserBO modifyUserBO)  {

        usersService.modifyUserinfo(modifyUserBO);
        // 获取用户的信息并且生成token信息
        UsersVO usersVO = usersService.getUserInfoById(modifyUserBO.getUserId(), true);

        return GraceJSONResult.ok(usersVO);
    }

    @PostMapping("/get")
    public GraceJSONResult get(@RequestParam  String userId)  {

        // 获取用户的信息
        UsersVO usersVO = usersService.getUserInfoById(userId, false);

        return GraceJSONResult.ok(usersVO);
    }


    @PostMapping("/updateFace")
    public GraceJSONResult<UsersVO> updateFace(@RequestParam  String userId, @RequestParam String face)  {
        log.info("userId, face, {}, {}", userId, face);
        if (StrUtil.isBlank(userId)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        if (StrUtil.isBlank(face)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        ModifyUserBO modifyUserBO = new ModifyUserBO();
        modifyUserBO.setUserId(userId)
                        .setFace(face);

        usersService.modifyUserinfo(modifyUserBO);
        // 获取用户的信息并且生成token信息
        UsersVO usersVO = usersService.getUserInfoById(modifyUserBO.getUserId(), true);

        return GraceJSONResult.ok(usersVO);
    }

    @PostMapping("/uploadFriendCircleBg")
    public GraceJSONResult<UsersVO> uploadFriendCircleBg(@RequestParam  String userId, @RequestParam String friendCircleBg)  {
        log.info("userId, friendCircleBg, {}, {}", userId, friendCircleBg);
        if (StrUtil.isBlank(userId)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        if (StrUtil.isBlank(friendCircleBg)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        ModifyUserBO modifyUserBO = new ModifyUserBO();
        modifyUserBO.setUserId(userId)
                .setFriendCircleBg(friendCircleBg);

        usersService.modifyUserinfo(modifyUserBO);
        // 获取用户的信息并且生成token信息
        UsersVO usersVO = usersService.getUserInfoById(modifyUserBO.getUserId(), true);

        return GraceJSONResult.ok(usersVO);
    }

    @PostMapping("/uploadChatBg")
    public GraceJSONResult<UsersVO> uploadChatBg(@RequestParam  String userId, @RequestParam String chatBg)  {
        log.info("userId, chatBg, {}, {}", userId, chatBg);
        if (StrUtil.isBlank(userId)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        if (StrUtil.isBlank(chatBg)){
            GraceJSONResult.errorCustom(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        ModifyUserBO modifyUserBO = new ModifyUserBO();
        modifyUserBO.setUserId(userId)
                .setChatBg(chatBg);

        usersService.modifyUserinfo(modifyUserBO);
        // 获取用户的信息并且生成token信息
        UsersVO usersVO = usersService.getUserInfoById(modifyUserBO.getUserId(), true);

        return GraceJSONResult.ok(usersVO);
    }


    @PostMapping("/queryFriend")
    public GraceJSONResult queryFriend(@RequestParam  String queryString, HttpServletRequest request)  {
        if (StrUtil.isBlank(queryString)){
            return GraceJSONResult.error();
        }

        // 获取用户的信息
        Users friend = usersService.getByWechatNumOrMobile(queryString);
        if (ObjectUtil.isNull(friend)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FRIEND_NOT_EXIST_ERROR);
        }
        // 获取当前登录用户的id
        String headerUserId = request.getHeader(RequestConstant.HEADER_USER_ID);
        // 不能添加自己为好友
        if (Objects.equals(friend.getId(), headerUserId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.CAN_NOT_ADD_SELF_FRIEND_ERROR);
        }


        return GraceJSONResult.ok(friend);
    }
}

